#include "frtimkiemdaily.h"
#include "ui_frtimkiemdaily.h"
#include "QtSql"

frtimkiemdaily::frtimkiemdaily(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frtimkiemdaily)
{
    ui->setupUi(this);
    db=new ketnoiDB();
}

frtimkiemdaily::~frtimkiemdaily()
{
    delete ui;
}

void frtimkiemdaily::on_btnTimKiem_clicked()
{
    if(ui->btnTimKiem->text()==trUtf8("Tìm"))
       {
           QString sql="select * from daily where  MaDaiLy like '%"+ui->txtTimDaiLy->text().trimmed()+"%' or TenDaiLy like '%"+ui->txtTimDaiLy->text().trimmed()+"%'";
           QSqlQueryModel *model = db->truyVanDuLieu(sql);
           model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
           model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên KH"));
           model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa Chỉ"));
           model->setHeaderData(3, Qt::Horizontal,trUtf8("Điện Thoại"));
           model->setHeaderData(4, Qt::Horizontal,trUtf8("Email"));
           ui->tblTimKiem->setModel(model);
           ui->tblTimKiem->show();
           if(!ui->txtTimDaiLy->text().trimmed().isEmpty())
           ui->btnTimKiem->setText(trUtf8("Thôi"));
       }
       else
       {
           loadDS();
           ui->btnTimKiem->setText(trUtf8("Tìm"));
           ui->txtTimDaiLy->clear();
       }

   }

void frtimkiemdaily::loadDS()

{      QSqlQueryModel *model = db->truyVanDuLieu("select * from daily");
       model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
       model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên KH"));
       model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa Chỉ"));
       model->setHeaderData(3, Qt::Horizontal,trUtf8("Điện Thoại"));
       model->setHeaderData(4, Qt::Horizontal,trUtf8("Email"));
       ui->tblTimKiem->setModel(model);
       ui->tblTimKiem->show();

}
